#include "surface.h"

#include "util/mmgr.h"
#include "util/log.h"				// log
#include "util/common_macros.h"		// macros
		    
 // resouce manager
#include "resman/resman.h"

// VFS
#include "vfs/vfs.h"

#include <cassert>

Surface::Surface() : 
	ResourceFile(),
	m_Height(0),
	m_Width(0),
	m_Size(0),
	m_IsCubemap(false)
{
	m_FileType  = RES_SURFACE;
}

Surface::~Surface()
{
	ReleaseResource();
}

unsigned int	Surface::GetWidth() 
{
    if( m_Width == 0 && m_Height == 0)
    {
        m_Width = 2;        // at least make sure not to crash
        CacheResource();    // it havn't init , try doing now
    }
 
        
    return m_Width;
}

unsigned int	Surface::GetHeight()
{
    
    if( m_Width == 0 && m_Height == 0)
    {
        m_Height = 2;    // at least make sure not to crash
        CacheResource(); // it havn't init , try doing now
    }
  
    return m_Height;
}

/// create resource as a hardware specific format
void			Surface::InitializeResource()
{

	if (m_State == RS_RELEASED)
	{
		return CacheResource();
	}

	if (m_State != RS_CACHED)
	{
		_LOG(MSG_WARNING, "Surface " << m_Name << " not yet loaded!");
		return;
	}
	if (m_Data.Size == 0)
	{
		CacheResource();
	}

	if (m_Data.Size>0)
	{
		InitTexture();
		// remember to free resource at the end of the process
		m_Data.Reset();	// free up the memory.
	}	   
}


/// remove all data from RAM, but keep track of origin so that it can be reaquired
void			Surface::ReleaseResource()
{
	m_Data.Reset();
	m_State = RS_RELEASED;
}

